<template>
    <ex-dialog width="1000px" class="ex-diolog" :title="title" :visible="visible" @closeDialog="handleClose">
        <el-form ref="form" :model="form" :rules="rules" label-width="180px">
            <el-form-item label="是否启用搜索插件功能:">
                <el-switch v-model="form.status" active-text="是" :active-value="'0'" inactive-text="否" :inactive-value="'1'"></el-switch>
            </el-form-item>
            <template v-if="form.status=='0'">
                <el-form-item label="选择插件:" prop="pluginId">
                    <el-select v-model="form.pluginId" placeholder="请选择" :style="{ width: inputWidth }" @change="pluginChange">
                        <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="选择页面组件:" prop="assemblyId">
                    <el-select v-model="form.assemblyId" placeholder="请选择" :style="{ width: inputWidth }">
                        <el-option v-for="item in optionsView" :key="item.id" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
            </template>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm('form')" :loading="sumitLoading">保存提交</el-button>
            <el-button @click="handleClose">取 消</el-button>
        </div>
    </ex-dialog>
</template>
  
<script>
import {
    homePageSearch,homePagePlugin,homePagePagePluginId,homePageSet
} from '@/api/incentiveManagement/adConfigNew'
export default {
    // dicts: ['asset_card_type'],
    name: "Dialog",
    props: {
        visible: Boolean,
        id: String,
        pathType: String,
    },
    data() {
        return {
            inputWidth: '290px',
            sumitLoading: false,
            loading: false,
            title: "搜索配置",
            form: {
                assemblyId: '',//组件ID
                pluginId: '', //插件ID
                status:'1', //是否启用搜索插件功能0启用 1禁用
            },
            options:[],//插件下拉
            optionsView:[],//组件下拉
            // 表单校验
            rules: {
                assemblyId: [{
                    required: true,
                    message: "请选择页面组件",
                    trigger: ['change', 'blur'],
                }],
                pluginId: [{
                    required: true,
                    message: "请选择插件",
                    trigger: ['change', 'blur'],
                }],
            },
        };
    },
    created() {
        this.getDetail()
        this.getHomePagePlugin()
    },
    methods: {
        //获取详情
        getDetail() {
            var data = {}
            homePageSearch(data).then(res => {
                this.form.status = res.result.status
                this.form.pluginId = res.result.pluginId
                if(this.form.pluginId!=''){
                    this.getHomePagePagePluginId(this.form.pluginId)
                    this.form.assemblyId = res.result.assemblyId
                }else{
                    this.form.assemblyId = res.result.assemblyId
                }
            })
        },
        //获取插件下拉列表
        getHomePagePlugin() {
            var data = {}
            homePagePlugin(data).then(res => {
                this.options = res.result
            })
        },
        //插件选择
        pluginChange(e){
            this.getHomePagePagePluginId(e)
            this.form.assemblyId = ''
        },
        //获取组件下拉
        getHomePagePagePluginId(pluginId){
            homePagePagePluginId(pluginId).then(res => {
                this.optionsView = res.result
            })
        },
        //取消
        handleClose(formName) {
            this.$emit('update:visible', false)
        },
        //确认
        handleConfirm() {
            this.$emit('success')
        },
        /** 执行按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    var data = {
                        ...this.form
                    }
                    this.sumitLoading = true
                    homePageSet(data).then(response => {
                        this.$message.success('操作成功');
                        this.$emit('success')
                        this.handleClose();
                        this.sumitLoading = false;
                    }).catch(() => {
                        this.sumitLoading = false;
                    });
                }
            });
        },
    }
};

</script>
<style scoped>
.main {
    background-color: #fff;
    border-radius: 8px;
    padding: 20px 20px;
}

.icon_yes {
    font-size: 28px;
    color: #67c23a;
}

.el-select {
    width: 640px;
}

.el-button {
    margin: 0 20px;
}
.select-flow{
    width: 500px;
    white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
}
::v-deep .el-tag.el-tag--info{
    max-width: 300px;
    white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
}
</style>
  